// pages/ExtensionApply/ExtensionApply.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    info_img: "",
    //-----模块
    //是否提交审核
    isCheck: false,

    //显示模块
    becomeType_1: false,
    becomeType_2: false,
    becomeType_3: false,
    becomeType_4: false,

    become_1_info_1: "",
    become_1_info_2: "",

    become_2_info_1: "0",
    become_2_info_2: "0",
    become_2_info_3: "",

    become_3_info_1: "0",
    become_3_info_2: "0",
    become_3_info_3: "",


    become_4_info_1: "",
    become_4_info_2: "",
    become_4_info_3: "",

    para_name: "",
    para_phone: "",
    para_weixin: "",

    //推广提示
    extensionTips: "",
    //专区商品
    areaGoods: [],
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getExtensionBanner();
    this.initExtension();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getExtensionBanner() {
    let urlStr = app.getNetAddresss('member.member.agentbase');
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            info_img: res.data.banner
          });
        } else {
          this.setData({
            info_img: ''
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  goHome() {
    wx.reLaunch({
      url: '/packageG/index/index'
    });
  },
  initExtension() {
    //是否提交审核
    this.setData({
      isCheck: false,
      becomeType_1: false,
      becomeType_2: false,
      becomeType_3: false,
      becomeType_4: false,
      become_1_info_1: "",
      become_1_info_2: "",
      become_2_info_1: "0",
      become_2_info_2: "0",
      become_2_info_3: "",
      become_3_info_1: "0",
      become_3_info_2: "0",
      become_3_info_3: "",
      become_4_info_1: "",
      become_4_info_2: "",
      become_4_info_3: '',
      para_name: "",
      para_phone: '',
      para_weixin: ''
    });
    // this.getExtension();
    this.getExtensionInfo();
  },
  // getExtension() {
  //   let that = this;
  //   let urlStr = app.getNetAddresss('member.member.getMemberRelationInfo');
  //   app._getNetWork({
  //     url: urlStr,
  //     success: function (resdata) {
  //       var res = resdata.data;
  //       if (res.result == 1) {
  //         that.setView(res.data);
  //       }
  //     },
  //     fail: function (res) {
  //       console.log(res.msg);
  //     }
  //   });
  // },
  //设置view 显示
  setView(data) {
    if (data.base_info.status == 0) {
      this.setBecomeType(data.become);
      this.setBecomeTypeView(data.become, data);
    } else if (data.base_info.status == 1){
      //显示已申请
      this.setData({
        becomeType_1: false,
        becomeType_2: false,
        becomeType_3: false,
        becomeType_4: false,
        isCheck: true
      });
    } else {
      wx.redirectTo({
        url: '/mircoApp/commission/distribution/index'
      })
    }
  },
  //设置类型显示
  setBecomeType(become) {
    if (become == 1) {
      this.setData({
        becomeType_1: true,
        becomeType_2: false,
        becomeType_3: false,
        becomeType_4: false
      });
    } else if (become == 2 || become == 3) {
      this.setData({
        becomeType_1: false,
        becomeType_2: true,
        becomeType_3: true,
        becomeType_4: true
      });
    } else {
      this.setData({
        becomeType_1: false,
        becomeType_2: false,
        becomeType_3: false,
        becomeType_4: false
      });
    }
  },
  //设置类型显示view
  setBecomeTypeView(become, data) {
    if (become == 1) {
      this.setData({
        become_1_info_1: data.base_info.parent_name,
        become_1_info_2: data.base_info.shop_name,
        para_name: data.base_info.realname,
        para_phone: data.base_info.mobile
      });
    } else if (become == 2 || become == 3) {
      // this.getExtensionInfo();
    }
  },
  getExtensionInfo() {
    let that = this;
    let urlStr = app.getNetAddresss('member.member.shareinfo');
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        let res = resdata.data;
        if (res.result == 1 && res.data) {
          if (res.data.no_check_mini_url) {
            wx.showToast({
              title: "无需申请",
              icon: "none",
              duration: 2000,
              success() {
                wx.reLaunch({
                  url: res.data.no_check_mini_url,
                  fail: (err) => {
                    // 防止跳转不了
                    wx.reLaunch({
                      url: "/packageG/index/index",
                    });
                  }
                });
              },

            });
            return;
          }

          this.setView(res.data);
          this.setData({
            become_3_info_1: res.data.become_term.become_moneycount ? res.data.become_term.become_moneycount : '',
            become_3_info_2: res.data.getCostTotalPrice ? res.data.getCostTotalPrice : '',
            become_2_info_1: res.data.become_term.become_ordercount ? res.data.become_term.become_ordercount : '',
            become_2_info_2: res.data.getCostTotalNum ? res.data.getCostTotalNum : '',
            extensionTips: res.data.tip ? res.data.tip : '',
            areaGoods: res.data.become_term.goodsinfo ? res.data.become_term.goodsinfo : '',
            become_term_id:res.data.become_term_id,
            become_selfmoney:res.data.become_selfmoney
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  paraNameinp(e) {
    let val = e.detail.value;
    this.setData({
      para_name: val
    });
  },
  paraPhoneinp(e) {
    let val = e.detail.value;
    this.setData({
      para_phone: val
    });
  },
  //状态1 点击
  become_1_click() {
    if (app._isTextEmpty(this.data.para_name)) {
      wx.showToast({
        icon: 'none',
        title: "请输入真实姓名",
        duration: 1000
      });
      return;
    }
    if (app._isTextEmpty(this.data.para_phone)) {
      wx.showToast({
        icon: 'none',
        title: "请输入手机号",
        duration: 1000
      });
      return;
    }
    if (app._isMoblie(this.data.para_phone)) {
      wx.showToast({
        icon: 'none',
        title: "请输入正确的手机号",
        duration: 1000
      });
      return;
    }
    let _mid = "";
    try {
      const value = wx.getStorageSync('mid');
      if (value) {
        _mid = value;
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
    let that = this;
    let urlStr = app.getNetAddresss('member.member.addAgentApply');
    urlStr += '&mid=' + _mid;
    urlStr += '&realname=' + this.data.para_name;
    urlStr += '&mobile=' + this.data.para_phone;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.getExtensionInfo(); //重新获取
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  gotoShopGood() {
    wx.navigateTo({
      url: '/packageA/member/extension/ExtensionGoods/ExtensionGoods'
    });
    //ExtensionGoods
  },
  //状态3 点击
  become_3_click() {
    wx.redirectTo({
      url: '/packageG/index/index'
    });
  },
});
